#import "Usage.h"

@implementation Usage

- (void)awakeFromNib{
		
	usagePath = [[NSBundle mainBundle] pathForResource:@"usage" ofType:@"txt"];
	
	array = [[NSString stringWithContentsOfFile:usagePath] componentsSeparatedByString:@"-"];
	
	NSString *verifica = [array objectAtIndex: 0];
	
	if([verifica isEqualToString:@"=== NO BLOCK ==="]){
	
		[mancanti setStringValue:@"Serialize"];
		
		[progress setIntValue: 20];
		
	}
	
	else{
		
		
	usage = [[array objectAtIndex: 0] intValue];
	
	if(usage == 0){
	
		[mancanti setStringValue:@"Expired"];
		
		[progress setIntValue: 0];
		
		
	}
	
	else{
			
			usage--;
			
		if(usage == 0){
			
			[mancanti setStringValue:@"Expired"];
			
			[progress setIntValue: 0];
			
			
			{
				int result = NSRunAlertPanel(@"this Version of Test has expired", 
											 @"please select Buy to buy on the site or Enter Serial to serialize this software ", 
											 @"Buy", @"Quit", nil);
				
				if( result == NSAlertDefaultReturn )
				{
					[[NSWorkspace sharedWorkspace] launchApplication: @"Buy-KVideoPod_Pro"];
				}
				
				NSLog(@"Trial as expired");
				
				[[NSApplication sharedApplication] terminate:self];
				
			}
			
		}
		
		else{
		
			[mancanti setStringValue:[NSString stringWithFormat:@"%d times trial", usage]];
			
			NSString *newTxt = [NSString stringWithFormat:@"%d", usage];
								
			[newTxt writeToFile: usagePath atomically:YES encoding: NSASCIIStringEncoding error: NULL];
			
		[progress setIntValue: usage];
		
	   }
	
	}
}
	
	[usagePath retain];
}

- (IBAction)verificaKey:(id)sender{
	
	if([key intValue] == keyApp){
		
		[mancanti setStringValue:@"Serialize"];
		
		[progress setIntValue: 20];
	
		NSString *keyVerificata = [NSString stringWithFormat:@"=== NO BLOCK ==="];
		
		[keyVerificata writeToFile: usagePath atomically: YES encoding: NSASCIIStringEncoding error: NULL];
		
		/*[[NSWorkspace sharedWorkspace] launchApplication: @"Buy-KVideoPod_Pro"];
		[NSBundle loadNibNamed:@"StartingPointsWindow" owner:NSApp];*/
		
		
		
	}
	
}

- (IBAction)blocca:(id)sender{
	
	NSString *keyVerificata = [NSString stringWithFormat:@"21"];
	
	[keyVerificata writeToFile: usagePath atomically: YES encoding: NSASCIIStringEncoding error: NULL];		

	[self awakeFromNib];
}

@end